<?php

class Core_Api_Storage
{
	/**
	 * <code>
	 * result: * array{
	 *   id=>1,
	 * 	 adapter=>Nuo_Storage_Local,
	 *   baseUrl=> /soap,
	 * 	 basePath=> /public
	 * }
	 * </code>
	 * @todo    Use serialize/unserialize instead of json_encode/decode.
	 * @todo    Allow configure random to use multiple storage instead of default.
	 * @access  public
	 * @param   string int
	 * @return  array
	 */
	function getConfig($id)
	{
		// check load default config
		if ($id)
		{
			$row = Nuo::getTable('core/storage') -> find(intval($id)) -> current();

			if (!$row)
			{
				require_once 'nuo/storage/exception.class.php';
				throw new Nuo_Storage_Exception('Could not load storage config with id "' . $id . '"');
			}

			$data = json_decode($row -> extra, 1);

			$data['id'] = $row -> storage_id;
			$data['adapter'] = 'Nuo_Storage_' . ucfirst($row -> type_id);

			return $data;

		}
		return $this -> getDefaultConfig();

	}

	/**
	 * <code>
	 * result: * array{
	 *   id=>1,
	 * 	 adapter=>Nuo_Storage_Local,
	 *   baseUrl=> /soap,
	 * 	 basePath=> /public
	 * }
	 * </code>
	 * @todo    Use serialize/unserialize instead of json_encode/decode.
	 * @todo    Allow configure random to use multiple storage instead of default.
	 * @access  public
	 * @param   string int
	 * @return  array
	 */
	function getDefaultConfig()
	{
		$table = Nuo::getTable('core/storage');

		$select = $table -> select() -> where('is_default=1');

		$row = $table -> fetchRow($select);

		if (!$row)
		{
			require_once 'nuo/storage/exception.class.php';
			throw new Nuo_Storage_Exception('Could not load storage config for default storage' . $id);
		}

		$data = json_decode($row -> extra, 1);

		$data['id'] = $row -> storage_id;
		$data['adapter'] = 'Nuo_Storage_' . ucfirst($row -> type_id);

		return $data;
	}

}
